#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
from engine.common import *
if sys.platform == "darwin":
    adbpath = "adb "
else:
    adbpath = "/usr/bin/adb "
if (sys.argv.__len__())<4:
    print( "参数个数少了")
    exit(0)
handset = sys.argv[1]
currentHandsetSnumber = sys.argv[2]
command = sys.argv[3]
resultF = sys.argv[4]

filePath = sys.argv[5]

# 3.淘宝联盟好物圈页面点击复制文案
# ##复制文案后计算md5 保存到目录 判断 是否存在这个目录 不存在保存 存在 推出  保存xy坐标 给下一步用
# 之前界面:淘宝联盟好物圈页面
# 期待界面:淘宝联盟好物圈页面
# 事前计算:复制文案MinY
# 事前结果:posx,posy
# 动作执行:touchTap
# 事后计算:out*calcMd5AndSave*/tbk/alimama/*./temp/淘宝联盟好物圈页面.txt
# 事后结果:./temp/淘宝联盟好物圈页面.txt
# 出错状态转移码:None

def calcMd5AndSave( handset,currentHandsetSnumber,resultF,filePath="./temp/result.txt",delay=5 ):
    ret = False
    print(handset)
    print(currentHandsetSnumber)
    setHandsetType(handset)
    setHandsetSno(currentHandsetSnumber)
    content = getAndSetMsgFromHandsetCliper("test",currentHandsetSnumber)
    dirMd5 = getMd5(content)
    if not os.path.exists(getHandsetFileSavePath(resultF)):
        os.makedirs(getHandsetFileSavePath(resultF))
    if not os.path.exists(getHandsetFileSavePath(resultF+dirMd5+"/")):
        #create it now and
        print(getHandsetFileSavePath(resultF+dirMd5+"/"))
        os.makedirs(getHandsetFileSavePath(resultF+dirMd5+"/"))
        with open(getHandsetFileSavePath(resultF  + "curmd5.txt"),'w') as f:
            print('wriete to file')
            f.write(dirMd5)
        with open(getHandsetFileSavePath(resultF+dirMd5+"/desc.txt"),'w') as f:
            print('wriete to file')
            f.write(content)
    else:
        print('exist dir   '+dirMd5+'   now then  abort this task now--------')
        ret = True

    with open(getHandsetFileSavePath(filePath),'w')as f:
        if ret == True:
            f.write('True')
            return True  #TRUE 状态转移
        else:
            f.write('False')
            return False


if command =="calcMd5AndSave".strip():
    calcMd5AndSave(handset,currentHandsetSnumber,resultF,filePath)
